package com.lz.football.module.techBar.view
{
	import com.common.utils.DisplayObjectUtil;
	import com.common.utils.Reflection;
	import com.greensock.TweenLite;
	import com.liangzhong.events.TechBarEvent;
	import com.liangzhong.model.AppData;
	import com.liangzhong.model.goods.vo.GoodsVo;
	import com.liangzhong.model.goods.vo.PlayerStarVo;
	
	import common.item.GoodsUseItem;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.display.SimpleButton;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	
	import lz.controls.Alert;
	import lz.controls.events.ListEvent;

	/**
	 *球员洗练 
	 * @author lake
	 * 
	 */	
	public class PlayerRefreshPanel extends TechBasePanel implements ITechPanel
	{
		/**
		 *物品显示容器 
		 */		
		private var container 			:MovieClip;
		/**
		 *球员卡容器 
		 */		
		private var playerCardContainer:MovieClip;
		/**
		 *开始
		 */		
		private var startBtn			:SimpleButton;
		/**
		 *需要欧元 
		 */		
		private var txtPrice			:TextField;
		/**
		 *洗练价格
		 * 灰 -- 黑 
		 */		
		private var prices				:Array = [0,10000,20000,40000,500000,2000000,10000000];
		/**
		 *规则 
		 */		
		private var ruleBtn			:SimpleButton;
		/**
		 *显示商品 
		 */		
		private var showGoodsPanel 	:ShowGoodsPanel;
		/**
		 *球员卡 
		 */		
		private var playerCard 		:*;
		/**
		 *球员列表 
		 */		
		private var playerList 		:Array = [];
		
		public function PlayerRefreshPanel()
		{
			super();
		}
		/**
		 *初始化 
		 * 
		 */	
		override public function initUI(view:Object=null):void
		{
			_symbolSkin = Reflection.createInstance("com.football.techbar.view.PlayerRefreshSkin");
			this.addChild(_symbolSkin);
			container = _symbolSkin.container;
			startBtn = _symbolSkin.startBtn;
			ruleBtn = _symbolSkin.ruleBtn;
			txtPrice = _symbolSkin.txtPrice;
			playerCardContainer = _symbolSkin.playerCardContainer;
			//商品
			showGoodsPanel = new ShowGoodsPanel();
			showGoodsPanel.initUI();
			showGoodsPanel.isShowSelect = true;
			container.addChild(showGoodsPanel);
			playerList = AppData.getInstance().playerCenterDataManager.getPlayerStarList();
			showGoodsPanel.goodsList = playerList;
			//球员卡
			playerCard = Reflection.createInstance("com.liangzhong.view.tips.PlayerCardTips");
			playerCard.cdata = new PlayerStarVo();
			playerCardContainer.addChild(playerCard);
			//事件
			addEvtListener();
			//效果
			effectMc = _symbolSkin.effectMc;
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
		}
		/**
		 *注册事件 
		 * 
		 */	
		override public function addEvtListener():void
		{
			showGoodsPanel.addEventListener(ListEvent.ITEM_CLICK,showItemClickHandler);
			startBtn.addEventListener(MouseEvent.CLICK,startClickHandler);
			ruleBtn.addEventListener(MouseEvent.CLICK,ruleClickHandler);
		}
		/**
		 *规则点击 
		 * @param e
		 * 
		 */		
		override protected function ruleClickHandler(e :MouseEvent):void
		{
			this.dispatchEvent(new TechBarEvent("rule_Click",5,true));
		}
		/**
		 *点击左边物品 
		 * @param e
		 * 
		 */		
		private function showItemClickHandler(e :ListEvent):void
		{
			var goodsVo :GoodsVo = e.item as GoodsVo;
			if(goodsVo == null)
			{
				return;
			}
			txtPrice.text = "" + prices[goodsVo.color - 1];
			playerCard.setPureData(goodsVo,playerCard.getChildAt(0));
			//特效球员信息
			if(effectMc)
			{
				effectMc.pmc.mc.txtName.text = "" + goodsVo.name;
				//大头像
				var bigImgBg :MovieClip = effectMc.pmc.mc.bigImgBg;
				var bigBData :BitmapData = goodsVo.bigBitmapData;
				var bigBitMap :Bitmap = new Bitmap(bigBData);
				DisplayObjectUtil.removeAllChildren(bigImgBg);
				if(bigImgBg)
				{
					bigImgBg.addChild(bigBitMap);
				}
			}
		}
		/**
		 *重设 恢复初始化 
		 * 
		 */		
		override public function reset():void
		{
			txtPrice.text = "";
			startBtn.mouseEnabled = true;
			showGoodsPanel.mouseChildren = true;
			showGoodsPanel.mouseEnabled = true;
			effectMc.visible = false;
			effectMc.gotoAndStop(1);
		}
		/**
		 *开始
		 * @param e
		 * 
		 */		
		private function startClickHandler(e :MouseEvent):void
		{
			var selectItem :GoodsUseItem = showGoodsPanel.selectItem;
			if(selectItem == null)
			{
				Alert.showErrorTip("请选择要洗练的球员");
				return;
			}
			var playerStarVo :PlayerStarVo = selectItem.data;
			if(playerStarVo.self == 1)
			{
				Alert.showErrorTip("不能洗练自身角色");
				return;
			}
			if(playerStarVo.level == 1)
			{
				Alert.showErrorTip("1级球员不能洗练");
				return;
			}
			var playerId :int = playerStarVo.playerid;
			this.dispatchEvent(new TechBarEvent(TechBarEvent.PLAYER_REFRESH,{id1:playerId},true));
			startBtn.mouseEnabled = false;
			showGoodsPanel.mouseChildren = false;
			showGoodsPanel.mouseEnabled = false;
		}
		/**
		 *删除事件 
		 * 
		 */	
		override public function removeEvtListener():void
		{
			showGoodsPanel.removeEventListener(ListEvent.ITEM_CLICK,showItemClickHandler);
			startBtn.removeEventListener(MouseEvent.CLICK,startClickHandler);
		}
		/**
		 *销毁 
		 * 
		 */	
		override public function destroyView():void
		{
			removeEvtListener();
			showGoodsPanel.destroyView();
			showGoodsPanel = null;
			TweenLite.killTweensOf(reset);
			TweenLite.killTweensOf(showResObj);
			TweenLite.killTweensOf(showAlertRes);
		}
		/////////////////////////server return//////////////////////////////////////////
		/**
		 *球员洗练
		 * 
		 */		
		public function playerRefresh(value :Object):void
		{
			if(value.retcode == 0)
			{
				var playerid :int = value.playerid;
				var starId :int = value.starid;
				TweenLite.delayedCall(3,showAlertRes,[playerid]);
				if(effectMc)
				{
					effectMc.visible = true;
					effectMc.gotoAndPlay(1);
				}
				TweenLite.delayedCall(3,reset);
				TweenLite.delayedCall(2.5,showResObj,[playerid,starId]);
			}
			else
			{
				reset();
			}
		}
		/**
		 *改变球员信息 
		 * @param playerid
		 * @param playerStarVo
		 * 
		 */		
		private function changePlayerInfo(playerid :int,pvo :PlayerStarVo):void
		{
			for each(var playervo :PlayerStarVo in playerList)
			{
				if(playervo.playerid == playerid)
				{
					playervo.level 		= 1;
					playervo.attack 	= pvo.attack;
					playervo.defence 	= pvo.defence;
					playervo.power 		= pvo.power;
					playervo.steal 		= pvo.steal;
					playervo.speed 		= pvo.speed;
					playervo.control 	= pvo.control;
					playervo.color 		= pvo.color;
					break;
				}
			}
		}
		/**
		 *显示成功物品 
		 * @param pid
		 * @param ntype
		 * 
		 */		
		private function showResObj(playerid:int,starId:int):void
		{
			if(playerid == 0)
			{
				return ;
			}
			if(playerid > 0)
			{
				var playerStarVo :PlayerStarVo = AppData.getInstance().goodsManager.getStarById(starId) as PlayerStarVo;
				playerStarVo.level = 1;
				playerCard.setPureData(playerStarVo,playerCard.getChildAt(0));
				changePlayerInfo(playerid,playerStarVo);
			}
		}
		/**
		 *显示成功失败 
		 * @param value
		 * 
		 */		
		private function showAlertRes(value :int ):void
		{
			if(value == 0)
			{
				Alert.showErrorTip("球员洗练失败");
			}
			else
			{
				Alert.showErrorTip("球员洗练成功");
			}
		}
	}
}